﻿
/*
(c) 2010 - Open Source Business Foundation e. V. (www.osbf.de)
This software is provided under Microsoft Reciprocal License (Ms-RL)
(http://www.microsoft.com/opensource/licenses.mspx#Ms-RL)
All other rights reserved
*/

using System.Collections.Generic;
using System.Data.Services.Providers;
using DataServiceProvider;
using MicroFormatProcessing;

namespace MicroFormatWebRole.OData {

    /// <summary>
    /// This class is the container for OData queried Microformats. 
    /// </summary>

    public class MfMicroformatDSPResourceList : MfDSPResourceList {
        private string UserId { get; set; }
        private string TagName { get; set;} 
        public DSPResource User {get;set;}
        public DSPResource Tag { get; set; }
 
        public MfMicroformatDSPResourceList(DSPContext context, DSPMetadata metadata, string userId, string tagName) : base(context, metadata){
            this.UserId = userId;
            this.TagName = tagName;
        }

        public MfMicroformatDSPResourceList(DSPContext context, DSPMetadata metadata, string userId) : base(context, metadata) {
            this.UserId = userId;
        }

        protected DSPResource CreateMicroformatResource(MicroFormat microformat, ResourceSet microformatsSet, ResourceType propertyType) {

            string[] microformatsKeyValueList = { Constants.ODATA_FIELD_KEY, Constants.ODATA_FIELD_VALUE, Constants.ODATA_FEED_KEY, Constants.ODATA_FEED_VALUE };
            string[] microformatsPropertyList = { Constants.ODATA_COLLECTIONOWNER_USER, Constants.ODATA_COLLECTIONOWNER_TAG, 
                                                  Constants.ODATA_FIELD_ROWKEY, Constants.ODATA_FIELD_USERID, 
                                                  Constants.ODATA_FIELD_TYPE, Constants.ODATA_FIELD_NAME, 
                                                  Constants.ODATA_FIELD_FOUNDONURL, Constants.ODATA_FIELD_FOUNDONDATE, 
                                                  Constants.ODATA_FIELD_PROPERTYNAMES, Constants.ODATA_FIELD_PROPERTYVALUES,
                                                  Constants.ODATA_FIELD_TAGS, Constants.ODATA_COLLECTIONS_MICROFORMATPROPERTIES, 
                                                  Constants.ODATA_FEED_ROWKEY };

            IList<DSPResource> mfProperties = new List<DSPResource>();
            DSPResource mfResource = CreateResourceObject(microformatsSet.ResourceType, microformatsPropertyList, User, Tag, microformat.RowKey,
                                                          microformat.GetUserId(), microformat.Type, microformat.Name, microformat.FoundOnUrl,
                                                          microformat.FoundOnDate, microformat.PropertyNames, microformat.PropertyValues,
                                                          microformat.Tags, mfProperties, microformat.RowKey);

            foreach (string mfKey in microformat.GetPropertyNameArray()) {
                string value = microformat.GetProperty(mfKey);
                mfProperties.Add(CreateResourceObject(propertyType, microformatsKeyValueList, mfKey, value, mfKey, value));
            }
            return mfResource;
        }

        protected override void LoadData() {
            MicroFormatRepository microformatsRepository = new MicroFormatRepository();

            ResourceSet microformatsSet;
            ResourceType propertyType;
            metadata.TryResolveResourceSet(Constants.ODATA_ENTITY_MICROFORMAT, out  microformatsSet);
            metadata.TryResolveResourceType(metadata.ContainerNamespace + "." + Constants.ODATA_ENTITY_MICROFORMATPOPERTIES, out propertyType);

            if (TagName == null || TagName =="") {

                foreach (MicroFormat microformat in microformatsRepository.FindByUserId(UserId)) {
                    Add(CreateMicroformatResource(microformat, microformatsSet, propertyType));
                }
            } else {

                TagRepository tagRepository = new TagRepository();
                foreach (Tag tag in tagRepository.FindByUserIdAndTagName(this.UserId, this.TagName)) {
                    foreach (string reference in tag.GetReferences()) {
                        foreach (MicroFormat microformat in microformatsRepository.FindByUserIdAndMfId(this.UserId, reference)) {
                            Add(CreateMicroformatResource(microformat, microformatsSet, propertyType));
                        }
                    }
                }
            }
        }

    }
}